/**
 * Copyright @ 2013 - 2015 Suntec Software(Shanghai) Co., Ltd.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are NOT permitted except as agreed by
 * Suntec Software(Shanghai) Co., Ltd.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#include "AppEntry.h"
#include "Import/QmlPlugin.h"
#include "ViewController.h"
#include "ngapp/App.h"
#include "qmlframework/NQAPI.h"
#include "ngbase/NGLog.h"

/**
 * @brief class NativeApp_Home.
 *
 * This class is to manage the native app.
 *
 * @see do something at NativeApp_Home::onCreate()
 */
class NativeApp_Home : public IApp
{
public:
    /// constructor.
    NativeApp_Home(AppId appId);

    /// destructor.
    ~NativeApp_Home();

    /// overriden from class IApp.
    NGViewId StartView();

    /// overriden from class IApp.
    NGCreator* Creator();

    /// overriden from class IApp.
    void onCreate(const nutshell::NIIntent *intent);

    /// overriden from class IApp.
    void onInitialize(const nutshell::NIIntent *intent);

    /// overriden from class IApp.
    void onLanguageChanged();

    /// overriden from class IApp.
    void onFinalize(const nutshell::NIIntent *intent);

    /// overriden from class IApp.
    void onForegroundEnter(const nutshell::NIIntent *intent);

private:
    /// private member.
    ViewController m_viewController;
};

NativeApp_Home::NativeApp_Home(AppId appId)
    : IApp(appId)
    , m_viewController(appId)
{
    NGLogDebug(APP_LogTag, "NativeApp_Home appId is %u", appId);
}

NativeApp_Home::~NativeApp_Home()
{
}

NGViewId NativeApp_Home::StartView()
{
    return m_viewController.startViewId();
}

NGCreator* NativeApp_Home::Creator()
{
    NGLogDebug(APP_LogTag, "NativeApp_Home::Creator");
    return &m_viewController;
}

void NativeApp_Home::onCreate(const nutshell::NIIntent *intent)
{
    Q_UNUSED(intent);
    NGLogDebug(APP_LogTag, "NativeApp_Home::onCreate");
    QmlPlugin plugin(m_appId);
    plugin.registerTypes();
    m_viewController.init();

    // uncomment if using MIST.
    // APP::IF(m_appId)->loadState("/QmlDemon/states.qml");
}

void NativeApp_Home::onInitialize(const nutshell::NIIntent *intent)
{
    Q_UNUSED(intent);
    NGLogDebug(APP_LogTag, "NativeApp_Home::onInitialize");
}

void NativeApp_Home::onLanguageChanged()
{
}

void NativeApp_Home::onFinalize(const nutshell::NIIntent *intent)
{
    Q_UNUSED(intent);
}

void NativeApp_Home::onForegroundEnter(const nutshell::NIIntent *intent)
{
    Q_UNUSED(intent);
}

///< definition of DECLARE_APPLICATION macro at ngapp/App.h
DECLARE_APPLICATION(NativeApp_Home)

/* EOF */
